package com.heima.wemedia.controller.v1;

import com.heima.common.exception.LeadNewsException;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmUserDto;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/8/3
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private WmUserService wmUserService;

    @PostMapping("/in")
    public ResponseResult login(@RequestBody WmUserDto wmUserDto){
        log.info("login by {} " + wmUserDto);

        //参数判断
        if (Objects.isNull(wmUserDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (StringUtils.isBlank(wmUserDto.getName())){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_USERNAME);
        }
        if (StringUtils.isBlank(wmUserDto.getPassword())){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_PASSWORD);
        }
        return wmUserService.login(wmUserDto);
    }

}